var offsetH = (heightClient)*0.02;
layui.use(['table', 'form'], function () {
    var table = layui.table,
        form = layui.form;
    var roleId = null;
    table.render(renderTable('#roleList', "role/page", [
        {type: 'numbers', title: '序号'},
        {field: 'roleName', title: '角色名称', sort: false, align: 'center'},
        {field: 'roleDesc', title: '角色描述', sort: false, align: 'center'},
        {field: 'modifyDate', title: '修改时间', sort: false, align: 'center'},
        {
            field: 'roleType', title: '角色类型', sort: false, align: 'center', templet: function (d) {
                if (d.roleType == 1) {
                    return '<span class="font-blue">系统默认角色</span>'
                } else if (d.roleType == 3) {
                    return '<span class="font-green">自定义角色</span>'
                } else
                    return '';
            }
        },
        {title: '操作', sort: false, align: 'center', toolbar: '#barDemo'}
    ]))
    table.on('tool(roleList)', function (obj) {
        var data = obj.data;
        roleId = data.roleId
        if (obj.event === "edit") {
            layer.open({
                type: 1,
                title: "编辑角色信息",
                offset: ['100px'],
                area: ['550px', '300px'],
                content: $("#roleDiv"),
                success: function () {
                    form.val('editForm', {
                        "roleName": data.roleName,
                        "roleDesc": data.roleDesc,
                        "roleType": data.roleType
                    })
                },
                end: function (i) {
                    layer.close(i)
                    $("#editForm")[0].reset()
                    $("#roleDiv").hide()
                    roleId = null;
                }
            });
        }
        if (obj.event === "del") {
            confirm('确定要删除该角色？', function () {
                postAjaxReq("role/delete", {roleId: roleId}, function (res) {
                    if (res.code == 1) {
                        layer.msg("操作成功")
                        table.reload('roleList');
                    } else {
                        layer.msg(res.msg)
                    }
                    roleId = null;
                })
            })
        }
        if (obj.event === "privilege") {
            layer.open({
                type: 1,
                title: "权限分配",
                btnAlign: "c",
                offset: [offsetH],
                area: ['800px', '500px'],
                content: $("#privilegeDiv"),
                success: function () {
                    getAjaxReq("role/privilegesByRoleId", {roleId: roleId}, function (res) {
                        if (res.code == 1) {
                            $(".menu").each(function (i, e) {
                                var list = res.data;
                                list.forEach(function (r) {
                                    if ($(e).data("id") == r.privilegeId)
                                        $(e).removeClass('icon-unchecked').addClass("icon-checked").attr("data-class", "icon-checked")
                                })
                            })
                        }
                    })
                },
                end: function (i) {
                    layer.close(i)
                    $("#privilegeDiv").hide()
                    $(".menu").each(function (i, e) {
                        $(e).removeClass("icon-checked").addClass("icon-unchecked").attr("data-class", "icon-unchecked")
                    })
                    roleId = null;
                }
            });
        }
        if (obj.event === "see") {
            layer.open({
                type: 1,
                title: "角色权限",
                btnAlign: "c",
                offset: [offsetH],
                area: ['800px', '600px'],
                content: $("#privilegeDiv"),
                success: function () {
                    $("#privilegeSubmit").hide();
                    $(".menu").each(function () {
                        $(this).addClass('disabled')
                    });
                    getAjaxReq("role/privilegesByRoleId", {roleId: roleId}, function (res) {
                        if (res.code == 1) {
                            $(".menu").each(function (i, e) {
                                var list = res.data;
                                list.forEach(function (r) {
                                    if ($(e).data("id") == r.privilegeId)
                                        $(e).removeClass('icon-unchecked').addClass("icon-checked").attr("data-class", "icon-checked")
                                })
                            })
                        }
                    })
                },
                end: function (i) {
                    layer.close(i)
                    $("#privilegeDiv").hide()
                    $(".menu").each(function (i, e) {
                        $(e).removeClass("icon-checked").addClass("icon-unchecked").attr("data-class", "icon-unchecked")
                        $(e).removeClass('disabled')
                    })
                    $("#privilegeSubmit").show()
                    roleId = null;
                }
            });
        }

    });
    form.on('submit(editSubmit)', function (obj) {
        var url = null
        var data = obj.field
        if (isEmpty(roleId)) {
            url = "role/save"
        } else {
            data.roleId = roleId
            url = "role/edit"
        }
        postAjaxReq(url, data, function (res) {
            if (res.code == 1) {
                layer.closeAll()
                table.reload("roleList");
                roleId = null;
                layer.msg("操作成功")
            } else {
                layer.msg(res.msg)
            }
        })
    });
    $("#privilegeSubmit").on("click", function () {
        var req = new Array();
        $(".menu").each(function (i, e) {
            if ($(e).hasClass("icon-checked"))
                req.push({
                    roleId: roleId,
                    privilegeId: $(e).data("id")
                })
        })
        if (req.length == 0) {
            req.push({
                roleId: roleId
            })
        }
        jsonAjaxReq("role/privilege/save", req, function (res) {
            if (res.code == 1) {
                layer.closeAll()
                layer.msg("操作成功")
                table.reload('roleList');
                roleId = null;
            } else {
                layer.msg(res.msg)
            }
        })
    })
    $("#addRole").on("click", function () {
        roleId = null;
        layer.open({
            type: 1,
            title: "新增角色信息",
            area: ['550px', '300px'],
            offset: ['100px'],
            content: $("#roleDiv"),
            end: function (i) {
                layer.close(i)
                $("#editForm")[0].reset()
                $("#roleDiv").hide()
            }
        });
    });

    $("#search").on("click", function () {
        var roleName = $("#roleName").val(),
            roleType = $("#roleType").val();
        if (roleType == -1) roleType = null
        table.reload('roleList', {
            page: {curr: 1},
            where: {
                roleName: roleName,
                roleType: roleType
            }
        })
    });
});
$("#cancel").on("click", function () {
    layer.closeAll()
    $("#editForm")[0].reset()
    $("#roleDiv").hide()
});
$("#privilegeCancel").on("click", function () {
    layer.closeAll()
    $("#privilegeDiv").hide()
    $(".menu").each(function (i, e) {
        $(e).removeClass("icon-checked").addClass("icon-unchecked").attr("data-class", "icon-unchecked")
    })
});